package com.hnl.action.talent;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.aware.HNLTalentAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.ReferralDAO;
import com.hnl.dao.TalentDAO;
import com.hnl.to.Referral;
import com.hnl.to.Talent;
import com.hnl.to.wrapper.ReferralWrapper;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadReferralByTalentID extends HttpServlet implements
		HNLTalentAware {
	public static final Logger LOG = LoggerFactory
			.getLogger(LoadReferralByTalentID.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			String talentID = req.getParameter(ATTRIBUTE_TALENT_ID);		
			Collection<ReferralWrapper> Referrals = new Vector<ReferralWrapper>();
			Talent oldTalent=null;
			if(talentID!=null&&talentID.length()!=0){
				ReferralDAO referralDAO = HNLDAOFactory.getDAOFactory(
						HNLDAOFactory.HNLDAOFACTORY).getReferralDAO();	
				TalentDAO talentDAO = HNLDAOFactory.getDAOFactory(
						HNLDAOFactory.HNLDAOFACTORY).getTalentDAO();	
				Collection<Referral> referralCollection=referralDAO.loadReferral(Integer.valueOf(talentID));
				for (Referral referral : referralCollection) {
					oldTalent=talentDAO.loadTalent(referral.getReferralId());
					if(!oldTalent.getIsdelete()){
						ReferralWrapper newReferral=new ReferralWrapper(referral);
						newReferral.getReferralName();
						Referrals.add(newReferral);
					}
				}				
			}
			
			writer.println(JSONResponseUtil.getJSONObjectString(Referrals));
		} catch (DAOException e) {
			LOG.error("error laoding Talent", e);
		} finally{
			writer.close();
		}
	}
}
